package internal

import (
	"file-system/internal/filter"
	"file-system/internal/handler"
	route2 "file-system/internal/handler/route"
	"github.com/gin-contrib/gzip"
	"github.com/gin-contrib/pprof"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func SetUpRouter() *gin.Engine {
	gin.SetMode(gin.DebugMode)

	r := gin.New()
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("session", store))
	r.Use(filter.CORS(), filter.RequestLogHandler(), filter.RecoverHandler())
	r.Use(gzip.Gzip(gzip.DefaultCompression))
	factories := []handler.RegisterFactory{
		route2.NewHandler,
	}

	for i := range factories {
		h, err := factories[i]()
		if err != nil {
			panic(err)
		}
		h.ApplyRoute(r)
	}

	pprof.Register(r)

	return r
}
